<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class MY_Controller extends MX_Controller
{
    private static $instance;
    
    function __construct()
    {
        parent::__construct();
        self::$instance =& $this;
        if ( method_exists( $this, '_init' ) ) $this->_init();
        $this->load->library('form_validation');
    }

    public static function &get_instance()
    {
        return self::$instance;
    }

    /**
     * 获取公共部分版块
     * @param ref 传递到视图的变量地址
     */
    public function get_common_section( &$data )
    {
        $title = isset($data['title']) ? $data['title'] : '';
        $extra_css = isset( $data['extra_css'] ) ? $data['extra_css'] : array();
        $extra_js = isset( $data['extra_js'] ) ? $data['extra_js'] : array();
        $data['header'] = Modules::Run( 'header', $title, $extra_css, $extra_js );
        
        $data['footer'] = Modules::Run( 'footer' );
    }

    /**
     * 魔法方法 减少模型与控制器之间的耦合度
     * @param string $name 调用的模型的方法名 规则为 model_+模型名称+模型方法
     * @param mixed $arguments 参数
     * @return mixed
     */
    function __call( $name, $arguments )
    {
        $tmp = explode( '_', $name );
        if ( count( $tmp ) >=3 && strtolower( $tmp['0'] ) == 'model' )
        {
            $modelName = "{$tmp[1]}_model";
            $modelMethod = implode( '_', array_slice( $tmp, 2 ) );
            if ( !isset( $this->$modelName ) ) $this->load->model( $modelName );
            return call_user_func_array( array( $this->$modelName, $modelMethod ), $arguments );
        }
        else
        {
            return '';
        }
    }

}